<?php
class ToolServer
{

    /**
     * 获取ip
     * @return array
     */
    public function getClientIp()
    {
        $remote_addr = getenv('REMOTE_ADDR');
        $http_client_ip = getenv('HTTP_CLIENT_IP');
        $http_x_forwarded_for = getenv('HTTP_X_FORWARDED_FOR');
        $http_via = getenv('HTTP_VIA');
        $ip_addr = array(
            'remote_addr' => $remote_addr ? $remote_addr : '',
            'http_client_ip' => $http_client_ip ? $http_client_ip : '',
            'http_x_forwarded_for' => $http_x_forwarded_for ? $http_x_forwarded_for : '',
            'http_via' => $http_via ? $http_via : ''
        );
        return $ip_addr;
    }

    public function getIp()
    {
        $ipArr = $this->getClientIp();
        $ip = empty($ipArr['http_x_forwarded_for']) ? $ipArr['remote_addr'] : $ipArr['http_x_forwarded_for'];
        return $ip;
    }

    /**
     * 查询字符是否存在于某字符串
     * @param string $needle 要查找的字符
     * @param string $haystack 被搜索的字符串
     * @return bool 返回true为找到，false未找到
     */
    function strExists($needle, $haystack)
    {
        return !(strpos($haystack, $needle) === FALSE);
    }

    /**
     * 成功返回
     * @param $info
     * @param array $data
     * @param int $code
     * @throws HttpResponseException
     */
    public function success($info, $data = [], $code = 1): void
    {
        header('Content-Type:application/json; charset=utf-8');
        $result = ['code' => $code, 'info' => $info, 'data' => $data];
        echo json_encode($result);
        die();
    }

    /**
     * 返回失败的操作
     * @param mixed $info 消息内容
     * @param mixed $data 返回数据
     * @param mixed $code 返回代码
     */
    public function error($info, $data = [], $code = 0): void
    {
        header('Content-Type:application/json; charset=utf-8');
        $result = [
            'code' => $code, 'info' => $info, 'data' => $data
        ];
        echo json_encode($result);
        die();
    }

    // 将传参转成数组化
    public function splitParam($url)
    {
        $paramsArr = [];
        $arr = explode('&', $url);
        foreach ($arr as $value) {
            $split = explode('=', $value);
            if (count($split) == 2) {
                $key = $split[0];
                if (strpos($key, '?') !== false) {
                    $key = explode('?', $key)[1];
                }
                $paramsArr[$key] = $split[1];
            }
        }
        return $paramsArr;
    }

    /**
     * 验证签名
     * @param array $params
     */
    public function checkSign($params = []){
        if(empty($params)){
            $params = $_REQUEST;
            if (empty($params)) {
                $params = json_decode(file_get_contents("php://input"), true);
            }
        }
        if(empty($params['signature'])){
            return '缺少签名参数';
        }
        if(empty($params['ts'])){
            return '缺少签名时间戳参数';
        }
        if($params['ts'] < time()-60 || $params['ts'] > time()+5){
            return '签名时间戳验证错误,超出：'.(time()-$params['ts']).'秒';
        }
        if(md5(substr($params['ts'],-4)) != $params['signature']){
            return '签名错误';
        }
        return true;
    }

    public function getForbidType(){
        return [
            0=>'验证通过',
            1=>'签名错误',
            2=>'oaid黑名单',
            3=>'IP黑名单',
            4=>''
        ];
    }

}
